//------------------------------------------------------------------------------
// The contents of this file are subject to the nopCommerce Public License Version 1.0 ("License"); you may not use this file except in compliance with the License.
// You may obtain a copy of the License at  http://www.nopCommerce.com/License.aspx. 
// 
// Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. 
// See the License for the specific language governing rights and limitations under the License.
// 
// The Original Code is nopCommerce.
// The Initial Developer of the Original Code is NopSolutions.
// All Rights Reserved.
// 
// Contributor(s): _______. 
//------------------------------------------------------------------------------

using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using NopSolutions.NopCommerce.BusinessLogic.Categories;
using NopSolutions.NopCommerce.BusinessLogic.Manufacturers;
using NopSolutions.NopCommerce.BusinessLogic.Media;
using NopSolutions.NopCommerce.BusinessLogic.Products;
using NopSolutions.NopCommerce.BusinessLogic.Products.Specs;
using NopSolutions.NopCommerce.BusinessLogic.Templates;
using NopSolutions.NopCommerce.Common.Utils;
using NopSolutions.NopCommerce.Web.Administration.Modules;
using NopSolutions.NopCommerce.BusinessLogic.Infrastructure;

namespace NopSolutions.NopCommerce.Web.Administration.Modules
{
    public partial class ProductFilesControl : BaseNopAdministrationUserControl
    {
        private void BindData()
        {
            var product = this.ProductService.GetProductById(this.ProductId);
            if (product != null)
            {
                pnlData.Visible = true;
                pnlMessage.Visible = false;

                var productFiles = product.ProductFiles;
                if (productFiles.Count > 0)
                {
                    gvwFiles.Visible = true;
                    gvwFiles.DataSource = productFiles;
                    gvwFiles.DataBind();
                }
                else
                    gvwFiles.Visible = false;
            }
            else
            {
                pnlData.Visible = false;
                pnlMessage.Visible = true;
            }

            txtDescription1.Text = "";
            txtDescription2.Text = "";
            txtDescription3.Text = "";
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                this.BindData();
            }
        }

        public void SaveInfo()
        {
        }

        protected void btnUploadProductFile_Click(object sender, EventArgs e)
        {
            try
            {
                var product = this.ProductService.GetProductById(this.ProductId);
                if(product != null)
                {
                    if(fuProductFile1.HasFile)
                    {
                      var File = new Download()
                      {
                        DownloadBinary = fuProductFile1.FileBytes,
                        ContentType = fuProductFile1.PostedFile.ContentType,
                        Filename = Path.GetFileNameWithoutExtension(fuProductFile1.PostedFile.FileName),
                        Extension = Path.GetExtension(fuProductFile1.PostedFile.FileName),
                        UseDownloadUrl = false,
                        IsNew = true
                      };
                      this.DownloadService.InsertDownload(File);

                      var productFile = new ProductFile()
                      {
                        ProductID = product.ProductId,
                        FileID = File.DownloadId,
                        DisplayOrder = txtProductFileDisplayOrder1.Value,
                        Description = txtDescription1.Text
                      };
                      this.ProductService.InsertProductFile(productFile);
                    }
                    if (fuProductFile2.HasFile)
                    {
                      var File = new Download()
                      {
                        DownloadBinary = fuProductFile2.FileBytes,
                        ContentType = fuProductFile2.PostedFile.ContentType,
                        Filename = Path.GetFileNameWithoutExtension(fuProductFile2.PostedFile.FileName),
                        Extension = Path.GetExtension(fuProductFile2.PostedFile.FileName),
                        UseDownloadUrl = false,
                        IsNew = true
                      };
                      this.DownloadService.InsertDownload(File);

                      var productFile = new ProductFile()
                      {
                        ProductID = product.ProductId,
                        FileID = File.DownloadId,
                        DisplayOrder = txtProductFileDisplayOrder2.Value,
                        Description = txtDescription2.Text
                      };
                      this.ProductService.InsertProductFile(productFile);
                    }
                    if (fuProductFile3.HasFile)
                    {
                      var File = new Download()
                      {
                        DownloadBinary = fuProductFile3.FileBytes,
                        ContentType = fuProductFile3.PostedFile.ContentType,
                        Filename = Path.GetFileNameWithoutExtension(fuProductFile3.PostedFile.FileName),
                        Extension = Path.GetExtension(fuProductFile3.PostedFile.FileName),
                        UseDownloadUrl = false,
                        IsNew = true
                      };
                      this.DownloadService.InsertDownload(File);

                      var productFile = new ProductFile()
                      {
                        ProductID = product.ProductId,
                        FileID = File.DownloadId,
                        DisplayOrder = txtProductFileDisplayOrder3.Value,
                        Description = txtDescription3.Text
                      };
                      this.ProductService.InsertProductFile(productFile);
                    }
                    
                    BindData();
                }
            }
            catch (Exception exc)
            {
                ProcessException(exc);
            }
        }

        protected void gvwFiles_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "UpdateProductFile")
            {
                int index = Convert.ToInt32(e.CommandArgument);
                GridViewRow row = gvwFiles.Rows[index];
                NumericTextBox txtProductFileDisplayOrder = row.FindControl("txtProductFileDisplayOrder") as NumericTextBox;
                TextBox txtProductFileDescription = row.FindControl("txtProductFileDescription") as TextBox;
                HiddenField hfProductFileId = row.FindControl("hfProductFileId") as HiddenField;

                int displayOrder = txtProductFileDisplayOrder.Value;
                int productFileId = int.Parse(hfProductFileId.Value);
                ProductFile productFile = this.ProductService.GetProductFileById(productFileId);

                if (productFile != null)
                {
                    productFile.DisplayOrder = displayOrder;
                    productFile.Description = txtProductFileDescription.Text;
                    this.ProductService.UpdateProductFile(productFile);
                }

                BindData();
            }
        }

        protected void gvwFiles_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                ProductFile productFile = (ProductFile)e.Row.DataItem;
                HyperLink hlFileName = e.Row.FindControl("hlFileName") as HyperLink;
                if (hlFileName != null)
                {
                  hlFileName.Text = productFile.NpFile.Filename + productFile.NpFile.Extension;
                  hlFileName.NavigateUrl = this.DownloadService.GetAdminDownloadUrl(productFile.NpFile);
                }
                      
                Button btnUpdate = e.Row.FindControl("btnUpdate") as Button;
                if (btnUpdate != null)
                    btnUpdate.CommandArgument = e.Row.RowIndex.ToString();
            }
        }

        protected void gvwFiles_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            int productFileId = (int)gvwFiles.DataKeys[e.RowIndex]["ProductFileId"];
            ProductFile productFile = this.ProductService.GetProductFileById(productFileId);
            if (productFile != null)
            {
                this.ProductService.DeleteProductFile(productFile.ProductFileID);
                this.DownloadService.DeleteDownload(productFile.FileID);
                BindData();
            }
        }

        protected override void OnPreRender(EventArgs e)
        {
            BindJQuery();
            this.btnMoreUploads.Attributes["onclick"] = "showUploadFPanels(); return false;";
        }

        public int ProductId
        {
            get
            {
                return CommonHelper.QueryStringInt("ProductId");
            }
        }
    }
}